package org.eerbaar.parser.cky;

import org.eerbaar.parser.core.def.Symbol;
import org.eerbaar.parser.core.exe.HasChildren;

public class SymbolPrinter {

    private String pad(int size) {
        StringBuffer s = new StringBuffer();
        for (int i = 0; i < size; i++) {
            s.append(" ");
        }
        return s.toString();
    }

    public String print(Symbol symbol) {
        return print(symbol, 0);
    }

    private String print(Symbol symbol, int depth) {
        String s = "";
        s += pad(depth) + symbol.getName() + "\n";
        if (symbol instanceof HasChildren) {
            HasChildren parent = (HasChildren) symbol;
            for (Symbol child : parent.getChildren()) {
                if (child instanceof HasChildren) {
                    HasChildren w = (HasChildren) child;
                    s += print((Symbol) w, depth + 2) + "\n";
                }
            }
        }
        return s;
    }

}
